function xdot=myssFun0526(t,x)
global N M In Ifo IMN r cord%swarm para
global L L1 Ld L1d Lup %topolohy
global LR L1R LupR %repaired topolohy
global A B A0 B0 Bbar%state matrix para
global T K1 %virtual layer para
global invPR PR Zetadot Zdot X0dot fol_a%private mat Epshat Epshatdot 
global Kcp Hcp P %CP layer param
global igx export tgx%middle var
global DoSP DoSR rho rho_R phi rhogx%DoS attacks
global dbar kesen % threshold of Actuation atatcks
global gbar1 tau_Ra 
global LupA LupRA ttg1x ttg1y tridx1
global Zetaall tau_Zbar
global eZ1 eZ2 eZ3 EpsZ1 EpsZ2 EpsZ3 XiZkeep h fuzhichi
global EpsZ1bar EpsZ2bar EpsZ3bar gg1x gg2x gg3x tritime1x tritime2x tritime3x
global tkp1ag1x tkag1x tkp1ag1y tkag1y tkp1ag2x tkag2x tkp1ag2y tkag2y tkp1ag3x tkag3x tkp1ag3y tkag3y 

rho = 0;
rho_R = 0;
rho_E1x=0;
rho_E1y=0;
rho_E2x=0;
rho_E2y=0;
rho_E3x=0;
rho_E3y=0;



for i = 1:size(DoSP,1)
    if (t>=DoSP(i,1))&&(t<DoSP(i,2))%&&(abs(tgx(igx)-tgx(igx-1)>0.00001))
        rho = 1;%under attack
    end
end

for i = 1:size(DoSR,1)
    if (t>=DoSR(i,1))&&(t<DoSR(i,2))%&&(abs(tgx(igx)-tgx(igx-1)>0.00001))
        rho_R = 1;%under repair
    end
end

Zeta = x(1:N*r*cord);%12*1
X=x(1+N*r*cord:N*r*cord+(M+N)*r*cord);%16*1
X0 = x(1+N*r*cord:r*cord+N*r*cord);%4*1
X1to3 = x(1+r*cord+N*r*cord:N*r*cord+(M+N)*r*cord);%12*1
rhohat = x(N*r*cord+(M+N)*r*cord+1:N*r*cord+(M+N)*r*cord+6);%6*1
Z1 = x(N*r*cord+(M+N)*r*cord+6+1:N*r*cord+(M+N)*r*cord+6+16);%16*1,agent1颁及璁＄zeta
Z2 = x(N*r*cord+(M+N)*r*cord+6+17:N*r*cord+(M+N)*r*cord+6+32);%16*1,agent2颁及璁＄zeta
Z3 = x(N*r*cord+(M+N)*r*cord+6+33:N*r*cord+(M+N)*r*cord+6+48);%16*1,agent3颁及璁＄zeta
%t
% eZ1
if t~=0
    bili =1;
    if (rho==0)||((rho==1)&&(rho_R==1))%?璁典?        if norm(eZ1(5:2:8), 2)>gbar1*norm(EpsZ1bar(1:2:4),2)/bili %xy寮
            Z1(1:2:end)=Zetaall(1:2:end);
            %ttg1x(igx)=tgx(igx);
            rho_E1x=1;
            
%             ggy=ggy+1;
% %             disp(t)
%             if rem(t/h,1)==0%rem(t/h,1)
%                 rho_E1x=1;
%                 ggx=ggx+1;
%                 tritime1x(:,ggx)=t;
%             end
        end
        if norm(eZ1(6:2:8), 2)>gbar1*norm(EpsZ1bar(2:2:4),2)/bili %xy寮
            Z1(2:2:end)=Zetaall(2:2:end);
            rho_E1y=1;
            %ttg1y(igx)=tgx(igx);
%             if rem(t/h,1)==0
%                 rho_E1y=1;
%             end
            
        end

        if norm(eZ2(9:2:12), 2)>gbar1*norm(EpsZ2bar(5:2:8),2)/bili
            Z2(1:2:end)=Zetaall(1:2:end);
            rho_E2x=1;
%             if rem(t/h,1)==0
%                 rho_E2x=1;
%             end
            
        end
        if norm(eZ2(10:2:12), 2)>gbar1*norm(EpsZ2bar(6:2:8),2)/bili
            Z2(2:2:end)=Zetaall(2:2:end);
            rho_E2y=1;
%             if rem(t/h,1)==0
%                 rho_E2y=1;
%             end
            
        end

        if norm(eZ3(13:2:16), 2)>gbar1*norm(EpsZ3bar(9:2:12),2)/bili
            Z3(1:2:end)=Zetaall(1:2:end);
            rho_E3x=1;
%             if rem(t/h,1)==0
%                 rho_E3x=1;
%             end
            
        end
        if norm(eZ3(14:2:16), 2)>gbar1*norm(EpsZ3bar(10:2:12),2)/bili
            Z3(2:2:end)=Zetaall(2:2:end);
            rho_E3y=1;
%             if rem(t/h,1)==0
%                 rho_E3y=1;
%             end
            
        end
end



% if t>DoSP(i,2)
%     rho = 1;
% end
%rho = 1;





%rhohat = x(N*r*cord+N*r*cord+(M+N)*r*cord+7:end);%6*1
h0 = [0;
    0;
    0;
    0];
h1 = [0.6*cos(0.2*t+(1-2)*2*pi/3);
    0.6*sin(0.2*t+(1-2)*2*pi/3);
    -0.12*sin(0.2*t+(1-2)*2*pi/3);
    0.12*cos(0.2*t+(1-2)*2*pi/3)];%hpx,hpy;hvx,hvy
h2 = [0.6*cos(0.2*t+(2-2)*2*pi/3);
    0.6*sin(0.2*t+(2-2)*2*pi/3);
    -0.12*sin(0.2*t+(2-2)*2*pi/3);
    0.12*cos(0.2*t+(2-2)*2*pi/3)];%hpx,hpy;hvx,hvy
h3 = [0.6*cos(0.2*t+(3-2)*2*pi/3);
    0.6*sin(0.2*t+(3-2)*2*pi/3);
    -0.12*sin(0.2*t+(3-2)*2*pi/3);
    0.12*cos(0.2*t+(3-2)*2*pi/3)];%hpx,hpy;hvx,hvy
hall = [h0' h1' h2' h3']'*5;%all


hfow = [h1' h2' h3']'*5;%all follower 12*1
% hall = zeros(16,1);
% hfow = zeros(12,1);
h0dot = [0;
    0;
    0;
    0];

h1dot = [-0.12*sin(0.2*t+(1-2)*2*pi/3);
    0.12*cos(0.2*t+(1-2)*2*pi/3);
    -0.024*cos(0.2*t+(1-2)*2*pi/3);
    -0.024*sin(0.2*t+(1-2)*2*pi/3);];%hpx,hpy;hvx,hvy
h2dot = [-0.12*sin(0.2*t+(2-2)*2*pi/3);
    0.12*cos(0.2*t+(2-2)*2*pi/3);
    -0.024*cos(0.2*t+(2-2)*2*pi/3);
    -0.024*sin(0.2*t+(2-2)*2*pi/3)];%hpx,hpy;hvx,hvy
h3dot = [-0.12*sin(0.2*t+(3-2)*2*pi/3);
    0.12*cos(0.2*t+(3-2)*2*pi/3);
    -0.024*cos(0.2*t+(3-2)*2*pi/3);
    -0.024*sin(0.2*t+(3-2)*2*pi/3)];%hpx,hpy;hvx,hvy
hdotall = [h0dot' h1dot' h2dot' h3dot']';%all
hdotfow = [h1dot' h2dot' h3dot']'*5;%all follower 12*1
% hdotfow = zeros(12,1);
%VLayer

Zeta0 = kron(invPR,In)*X0;
Zetaall=[Zeta0' Zeta']';%16*1
% Zetaall1=Z1;%16*1
% Zetaall2=Z2;%16*1
% Zetaall3=Z3;%16*1


VarPi=-kron(Ifo,kron(A0*invPR,In))*hfow+kron(Ifo,kron(invPR,In))*hdotfow;%12*1
VarPiall =[zeros(4,1)' VarPi']';
% Epsbar = [X0 Z]'-kron(IMN,kron(invPR,In))*hfunc(0);
% itoa = -kron(Lup,IMN)*Epsbar;%12*1
%Epsbar = Zetaallreal-kron(IMN,kron(invPR,In))*hall;%16*1
EpsbarZ1 = Z1-kron(IMN,kron(invPR,In))*hall;%16*1
EpsbarZ2 = Z2-kron(IMN,kron(invPR,In))*hall;%16*1
EpsbarZ3 = Z3-kron(IMN,kron(invPR,In))*hall;%Zetaall涓?ok锛㈡Zi灏卞?%psai = -kron(LupR,IMN)*EpsbarZ;
% if rho_R==1
%     Eps = -kron(LupR,IMN)*Epsbar*(1-rho)+rho*(-kron(LupRA,IMN)*Epsbar);%12*1
% else
%     Eps = -kron(Lup,IMN)*Epsbar*(1-rho)+rho*(-kron(LupA,IMN)*Epsbar);%12*1
% end

if rho_R==1
    %EpsZ = -kron(LupR,IMN)*EpsbarZ*(1-rho)+rho*(-kron(LupRA,IMN)*EpsbarZ);%12*1
    EpsZ1 = -kron(LupR,IMN)*EpsbarZ1*(1-rho)+rho*(-kron(LupRA,IMN)*EpsbarZ1);%12*1
    EpsZ2 = -kron(LupR,IMN)*EpsbarZ2*(1-rho)+rho*(-kron(LupRA,IMN)*EpsbarZ2);%12*1
    EpsZ3 = -kron(LupR,IMN)*EpsbarZ3*(1-rho)+rho*(-kron(LupRA,IMN)*EpsbarZ3);%12*1
    EpsZ1bar = -kron(LupR,IMN)*EpsbarZ1;%12*1
    EpsZ2bar = -kron(LupR,IMN)*EpsbarZ2;%12*1
    EpsZ3bar = -kron(LupR,IMN)*EpsbarZ3;%12*1
else
    %EpsZ = -kron(Lup,IMN)*EpsbarZ*(1-rho)+rho*(-kron(LupA,IMN)*EpsbarZ);%12*1
    EpsZ1 = -kron(Lup,IMN)*EpsbarZ1*(1-rho)+rho*(-kron(LupA,IMN)*EpsbarZ1);%12*1
    EpsZ2 = -kron(Lup,IMN)*EpsbarZ2*(1-rho)+rho*(-kron(LupA,IMN)*EpsbarZ2);%12*1
    EpsZ3 = -kron(Lup,IMN)*EpsbarZ3*(1-rho)+rho*(-kron(LupA,IMN)*EpsbarZ3);%12*1
    EpsZ1bar = -kron(Lup,IMN)*EpsbarZ1;%12*1
    EpsZ2bar = -kron(Lup,IMN)*EpsbarZ2;%12*1
    EpsZ3bar = -kron(Lup,IMN)*EpsbarZ3;%12*1
end

% Epsbardot = [kron(invPR,In)*X0dot; Zdot]-kron(IMN,kron(invPR,In))*hdotall;%16*1
% Epsdot = -kron(Lup,IMN)*Epsbardot;%12*1

% if t == 0
%     Eps0 = Eps
%     pause
% end
%%
%杈轰?涓磋绠缁
Zetaallreal=[Zeta0' Zeta']';%16*1
Epsbarreal = Zetaallreal-kron(IMN,kron(invPR,In))*hall;%16*1
Epsreal = -kron(Lup,IMN)*Epsbarreal;%12*1
export(igx,:) = Epsreal;
rhogx(igx)=rho;
%%
XiZ1=kron(Ifo,kron(B0'*K1,In))*EpsZ1;%12*1
XiZ2=kron(Ifo,kron(B0'*K1,In))*EpsZ2;%12*1
XiZ3=kron(Ifo,kron(B0'*K1,In))*EpsZ3;%12*1


XiZ=[XiZ1(1:4)' XiZ2(5:8)' XiZ3(9:12)']';
if (rho==1)&&(rho_R==0)%版诲讳淇?    %XiZ
    XiZ=zeros(12,1);
end

if t==0
    XiZkeep=XiZ;
end

% if t\in []
%     XiZreal=XiZreal;
% end
if (rho_E1x==1)&&(t-tkp1ag1x>=tau_Zbar)
    tkag1x=tkp1ag1x;
    tkp1ag1x=t;
    gg1x=gg1x+1
    tritime1x(:,gg1x)=t;
    XiZkeep(1:2:4)=XiZ(1:2:4);
end
if (rho_E1y==1)&&(t-tkp1ag1y>=tau_Zbar)
    tkag1y=tkp1ag1y;
    tkp1ag1y=t; 
    XiZkeep(2:2:4)=XiZ(2:2:4);
end

if (rho_E2x==1)&&(t-tkp1ag2x>=tau_Zbar)
    tkag2x=tkp1ag2x;
    tkp1ag2x=t;
    gg2x=gg2x+1;
    tritime2x(:,gg2x)=t;
    XiZkeep(5:2:8)=XiZ(5:2:8);
end
if (rho_E2y==1)&&(t-tkp1ag2y>=tau_Zbar)
    tkag2y=tkp1ag2y;
    tkp1ag2y=t;
    XiZkeep(6:2:8)=XiZ(6:2:8);
end

if (rho_E3x==1)&&(t-tkp1ag3x>=tau_Zbar)
    tkag3x=tkp1ag3x;
    tkp1ag3x=t;
    gg3x=gg3x+1;
    tritime3x(:,gg3x)=t;
    XiZkeep(9:2:12)=XiZ(9:2:12);
end
if (rho_E3y==1)&&(t-tkp1ag3y>=tau_Zbar)
    tkag3y=tkp1ag3y;
    tkp1ag3y=t;
    XiZkeep(10:2:12)=XiZ(10:2:12);
end

Zetadot = kron(Ifo,kron(A0,In))*Zeta+XiZkeep+VarPi;%12*1XiZ涓姝ｅ父棣?Z1dot = kron(IMN,kron(A0,In))*Z1+VarPiall;%12*1kron(Ifo,kron(A0,In))*Zeta+VarPi;%16*1 
Z1dot = kron(IMN,kron(A0,In))*Z1+VarPiall;%12*1kron(Ifo,kron(A0,In))*Zeta+VarPi;%16*1 
Z2dot = kron(IMN,kron(A0,In))*Z2+VarPiall;%12*1kron(Ifo,kron(A0,In))*Zeta+VarPi;%16*1 
Z3dot = kron(IMN,kron(A0,In))*Z3+VarPiall;%12*1kron(Ifo,kron(A0,In))*Zeta+VarPi;%16*1 

eZ1=Z1-Zetaall;%16*1
eZ2=Z2-Zetaall;%16*1
eZ3=Z3-Zetaall;%16*1




Zdot=[Z1dot' Z2dot' Z3dot']';%48*1
% eZ1 = Z1(5:end)-Zeta;%12*1
% eZ2 = Z2(5:end)-Zeta;
% eZ3 = Z3(5:end)-Zeta;
% for i=1:N %浜浠堕┍
%     if norm(eZ(4*(i-1)+1:4*i),2)>=gbar1*norm(psai(4*(i-1)+1:4*i),2)
%         Z(4*(i-1)+1:4*i)=Zeta(4*(i-1)+1:4*i);
%     end 
% end

% for i=1:N %浜浠堕┍
%     if norm(eZ(4*(i-1)+1:2:4*i),2)>=gbar1*norm(psai(4*(i-1)+1:2:4*i),2)
%         Z(4*(i-1)+1:2:4*i)=Zeta(4*(i-1)+1:2:4*i);
%     end
%     if norm(eZ(4*(i-1)+2:2:4*i),2)>=gbar1*norm(psai(4*(i-1)+2:2:4*i),2)
%         Z(4*(i-1)+2:2:4*i)=Zeta(4*(i-1)+2:2:4*i);
%     end
% end
% for i=1:N %浜浠堕┍
%     if norm(eZ(4*(i-1)+1:4*i),2)>=gbar1*norm(psai(4*(i-1)+1:4*i),2)
%         %Z(4*(i-1)+1:4*i)=Zeta(4*(i-1)+1:4*i);%Epsreal
%         Z(4*(i-1)+1:4*i)=Epsreal(4*(i-1)+1:4*i);
%         
%         if i==1
%             %tridx1=igx;
%             ttg1(igx)=tgx(igx);
%         end
%         
%         %Zdot(4*(i-1)+1:4*i)=zeros(4,1);
%         %i
%     end
%     
% end
%t


% for i=1:N %浜浠堕┍
%     if norm(eZ(4*(i-1)+1:4*i),2)>=gbar1*norm(psai(4*(i-1)+1:4*i),2)
%         Z=Zeta;
%         %i
%     end
% end



%CP layer
%AA attacks signals
Sigma = X1to3-kron(Ifo,kron(PR,In))*Zeta;%12*1
Sigma1x = Sigma(1:2:4);
Sigma1y = Sigma(2:2:4);
Sigma2x = Sigma(5:2:8);
Sigma2y = Sigma(6:2:8);
Sigma3x = Sigma(9:2:12);
Sigma3y = Sigma(10:2:12);%2*1
kesen = 0.01;

%dbar = 1;fuzhichi

if norm(Sigma1x'*P*B)>=fuzhichi
    rhohat1x = norm(Sigma1x'*P*B,2)+2*kesen;
else
    rhohat1x = norm(Sigma1x'*P*B,2)+2*kesen;%%+2*kesen*norm(Sigma1x'*P*B)/fuzhichi;
end
if norm(Sigma1y'*P*B)>=fuzhichi
    rhohat1y = norm(Sigma1y'*P*B,2)+2*kesen;
else
    rhohat1y = norm(Sigma1y'*P*B,2)+2*kesen;%%+2*kesen*norm(Sigma1y'*P*B)/fuzhichi;
end

if norm(Sigma2x'*P*B)>=fuzhichi
    rhohat2x = norm(Sigma2x'*P*B,2)+2*kesen;
else
    rhohat2x = norm(Sigma2x'*P*B,2)+2*kesen;%%+2*kesen*norm(Sigma2x'*P*B)/fuzhichi;
end
if norm(Sigma2y'*P*B)>=fuzhichi
    rhohat2y = norm(Sigma2y'*P*B,2)+2*kesen;
else
    rhohat2y = norm(Sigma2y'*P*B,2)+2*kesen;%%+2*kesen*norm(Sigma3y'*P*B)/fuzhichi;
end
    
if norm(Sigma2x'*P*B)>=fuzhichi
    rhohat3x = norm(Sigma3x'*P*B,2)+2*kesen;
else
    rhohat3x = norm(Sigma3x'*P*B,2)+2*kesen;%%+2*kesen*norm(Sigma3x'*P*B)/fuzhichi;
end
if norm(Sigma2y'*P*B)>=fuzhichi
    rhohat3y = norm(Sigma3y'*P*B,2)+2*kesen;%%+2*kesen;
else
    rhohat3y = norm(Sigma3y'*P*B,2)+2*kesen;%%+2*kesen*norm(Sigma3y'*P*B)/fuzhichi;
end


%rhohat1x = norm(Sigma1x'*P*B,2)+kesen;
%pause
chihat1x=softsign(Sigma1x'*P*B, kesen)*rhohat(1,:);
%rhohat1y = norm(Sigma1y'*P*B,2)+kesen;
chihat1y=softsign(Sigma1y'*P*B, kesen)*rhohat(2,:);

%rhohat2x = norm(Sigma2x'*P*B,2)+kesen;
chihat2x=softsign(Sigma2x'*P*B, kesen)*rhohat(3,:);
%rhohat2y = norm(Sigma2y'*P*B,2)+kesen;
chihat2y=softsign(Sigma2y'*P*B, kesen)*rhohat(4,:);

%rhohat3x = norm(Sigma3x'*P*B,2)+kesen;
chihat3x=softsign(Sigma3x'*P*B, kesen)*rhohat(5,:);
%rhohat3y = norm(Sigma3y'*P*B,2)+kesen;
chihat3y=softsign(Sigma3y'*P*B, kesen)*rhohat(6,:);

Chihat = [chihat1x chihat1y chihat2x chihat2y chihat3x chihat3y]';
rhohatdot = [rhohat1x rhohat1y rhohat2x rhohat2y rhohat3x rhohat3y]';
Chi=0.01*2*[2*t t -t -2*t 2*t -t]';%6*1
X0dot = kron(A0,In)*X0;%4*1
Vartheta = kron(Ifo,kron(Bbar*A0,In))*hfow - kron(Ifo,kron(Bbar,In))*hdotfow;%6*1
%Chihatdot1 = softsign(Sigma1'*P*B, kesen)*rhohat1;%2*1
% Chihatdot2 = softsign(Sigma2'*P*B, 0.02)*rhohat2;%2*1
% Chihatdot3 = softsign(Sigma3'*P*B, 0.02)*rhohat3;%2*1
u1to3 = kron(Ifo,kron(Kcp,In))*X1to3+kron(Ifo,kron(Hcp*PR,In))*Zeta-Vartheta+Chi-Chihat;%6*1
Xdotfol = kron(Ifo,kron(A,In))*X1to3+kron(Ifo,kron(B,In))*u1to3;%12*1
Xdot=    [X0dot' Xdotfol']';%(4+12)*1
fol_a(igx,:) = Xdotfol';




xdot=[Zetadot' Xdot' rhohatdot' Zdot']';%(12+16+6+48)*1=82

end

